﻿/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2023-09-20
 * 时间: 21:13
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

using System.Diagnostics;
using System.IO;
using System.Text;

namespace MediasInfo
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public Process Pro = new Process();
		public List<Dictionary<string, string>> Infos = new List<Dictionary<string, string>>();
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		Dictionary<string, string> GetInfo(string file)
		{
			Dictionary<string, string> info = new Dictionary<string, string>();

			try {
				string output = string.Empty;
				
				Pro.StartInfo.FileName = Application.StartupPath + "\\MediaInfo.exe" ;
	            Pro.StartInfo.Arguments = "\"" + file + "\"";
	            Pro.StartInfo.RedirectStandardOutput = true;
	            Pro.StartInfo.StandardOutputEncoding = Encoding.UTF8;
	            Pro.StartInfo.UseShellExecute = false;
	            Pro.StartInfo.CreateNoWindow = true;
	            
	            // 启动程序
	            Pro.Start();
	
	            // 同步读取标准输出
	            output = Pro.StandardOutput.ReadToEnd();
	
	            // 等待程序退出
	            Pro.WaitForExit();
	            
	            // MediaInfo标准输出字符串转字典
	            if (!string.IsNullOrWhiteSpace(output)) {
					string prefix = "";
					
					string[] kvs = output.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
					foreach (string s in kvs) {
						string[] kv = s.Split(new string[] { " : " }, StringSplitOptions.RemoveEmptyEntries);
						if (kv.Length == 2) {
							info.Add(prefix + "-" + kv[0].Trim(), kv[1].Trim());
						} else if (kv.Length == 1) {
							prefix = kv[0].Trim();
							if (!string.IsNullOrEmpty(prefix)) {
								info.Add(prefix, string.Empty);
							}
						}
					}
	            }
			} catch (Exception ex) {}

			return info;
		}
		
		void MainFormDragDrop(object sender, DragEventArgs e)
		{
			Infos.Clear();
			LV.Columns.Clear();
			LV.Items.Clear();
			
			ColumnHeader chTitle = new ColumnHeader();
			chTitle.Text = string.Empty;
	        chTitle.Width = 300;
	        LV.Columns.Add(chTitle);
			
            // 获取拖拽的文件路径数组
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
			
            
            int max = 0;
            Dictionary<string, string> firstColumn = null;
            foreach (string file in files) {
                Dictionary<string, string> info = GetInfo(file);
                if (info.Count > 0) {
                    if (info.Count > max) {
                        max = info.Count;
                        firstColumn = info;
                    }
                    Infos.Add(info);
                    
                    ColumnHeader ch = new ColumnHeader();
                    ch.Text = Path.GetFileName(file);
                    ch.Width = 380;		        
                    LV.Columns.Add(ch);
                }
            }
            
            if (firstColumn != null) {
                foreach (var pair in firstColumn) {
                    ListViewItem item = null;
                    if (string.IsNullOrEmpty(pair.Value)) {
                        item = new ListViewItem(pair.Key);
                        item.BackColor = Color.FromArgb(240, 240, 240);
                    } else {
                        item = new ListViewItem(" " + pair.Key.Substring(pair.Key.IndexOf('-') + 1));
                    foreach (Dictionary<string, string> d in Infos) {
                            item.SubItems.Add(d.ContainsKey(pair.Key) ? d[pair.Key] : string.Empty);
                    }
                    }
                    LV.Items.Add(item);
                }
            }
		}
		
		void MainFormDragEnter(object sender, DragEventArgs e)
		{
            // 确定拖拽操作是否包含文件
            e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop) ? DragDropEffects.Copy : DragDropEffects.None;   
		}
	}
}
